package ywpt.web.action.config;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;

import org.ecside.table.limit.Limit;
import org.ecside.util.RequestUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import ywpt.components.acegi.domain.Operation;
import ywpt.core.Constants;
import ywpt.core.SystemMessage;

import ywpt.core.exception.UniqueConstraintException;
import ywpt.core.util.StringTools;
import ywpt.core.web.BaseAction;
import ywpt.model.WeixinAccount;
import ywpt.model.ProjectPeople;
import ywpt.service.config.WeixinAccountService;
import ywpt.web.common.OperationConstants;
import ywpt.web.common.PublicFunction;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class WeixinAccountAction extends BaseAction implements ModelDriven<WeixinAccount>, Preparable{

	/**
	 * 微信账号管理Action
	 */
	private static final long serialVersionUID = 3076498333666668439L;
	private boolean[] popedom=new boolean[Constants.POPEDOM_LEN];
	protected static int DEFAULT_PAGE_SIZE	=100;
	private Long id;
	private WeixinAccount weixinAccount;
	private List<WeixinAccount> weixinAccountList = new ArrayList<WeixinAccount>();
	private WeixinAccountService  weixinAccountService;
	private int version;
	
	private Long weixin_type_id;
	private Long is_Certified_id;
	private Long unit_id;
	private String unit_name;
	
	private String[] peoId;
	
	//查询相关
	private Long qunit_id;
	
	private String qName;
	private Long qweixin_type;
	private Long qis_Certified;
	/**
	 * @return id
	 */
	public Long getId() {
		return id;
	}
	/**
	 * @param id 要设置的 id
	 */
	public void setId(Long id) {
		this.id = id;
	}
	/**
	 * @return is_Certified
	 */

	/**
	 * @return peoId
	 */
	public String[] getPeoId() {
		return peoId;
	}
	/**
	 * @param peoId 要设置的 peoId
	 */
	public void setPeoId(String[] peoId) {
		this.peoId = peoId;
	}
	/**
	 * @return popedom
	 */
	public boolean[] getPopedom() {
		return popedom;
	}
	/**
	 * @param popedom 要设置的 popedom
	 */
	public void setPopedom(boolean[] popedom) {
		this.popedom = popedom;
	}
	/**
	 * @return qis_Certified
	 */
	public Long getQis_Certified() {
		return qis_Certified;
	}
	/**
	 * @param qis_Certified 要设置的 qis_Certified
	 */
	public void setQis_Certified(Long qis_Certified) {
		this.qis_Certified = qis_Certified;
	}
	/**
	 * @return qName
	 */
	public String getQName() {
		return qName;
	}
	/**
	 * @param name 要设置的 qName
	 */
	public void setQName(String name) {
		qName = name;
	}
	/**
	 * @return qunit_id
	 */
	public Long getQunit_id() {
		return qunit_id;
	}
	/**
	 * @param qunit_id 要设置的 qunit_id
	 */
	public void setQunit_id(Long qunit_id) {
		this.qunit_id = qunit_id;
	}
	/**
	 * @return qweixin_type
	 */
	public Long getQweixin_type() {
		return qweixin_type;
	}
	/**
	 * @param qweixin_type 要设置的 qweixin_type
	 */
	public void setQweixin_type(Long qweixin_type) {
		this.qweixin_type = qweixin_type;
	}
	/**
	 * @return unit_id
	 */
	public Long getUnit_id() {
		return unit_id;
	}
	/**
	 * @param unit_id 要设置的 unit_id
	 */
	public void setUnit_id(Long unit_id) {
		this.unit_id = unit_id;
	}
	/**
	 * @return unit_name
	 */
	public String getUnit_name() {
		return unit_name;
	}
	/**
	 * @param unit_name 要设置的 unit_name
	 */
	public void setUnit_name(String unit_name) {
		this.unit_name = unit_name;
	}
	/**
	 * @return version
	 */
	public int getVersion() {
		return version;
	}
	/**
	 * @param version 要设置的 version
	 */
	public void setVersion(int version) {
		this.version = version;
	}
	/**
	 * @return weixin_type
	 */


	/**
	 * @return weixinAccount
	 */
	public WeixinAccount getWeixinAccount() {
		return weixinAccount;
	}
	/**
	 * @param weixinAccount 要设置的 weixinAccount
	 */
	public void setWeixinAccount(WeixinAccount weixinAccount) {
		this.weixinAccount = weixinAccount;
	}
	/**
	 * @return weixinAccountList
	 */
	public List<WeixinAccount> getWeixinAccountList() {
		return weixinAccountList;
	}
	/**
	 * @param weixinAccountList 要设置的 weixinAccountList
	 */
	public void setWeixinAccountList(List<WeixinAccount> weixinAccountList) {
		this.weixinAccountList = weixinAccountList;
	}
	/**
	 * @return weixinAccountService
	 */
	public WeixinAccountService getWeixinAccountService() {
		return weixinAccountService;
	}
	/**
	 * @param weixinAccountService 要设置的 weixinAccountService
	 */
	public void setWeixinAccountService(WeixinAccountService weixinAccountService) {
		this.weixinAccountService = weixinAccountService;
	}
	
	public WeixinAccount getModel() {
		// TODO 自动生成方法存根
		return this.weixinAccount;
	}
	
	public void prepare() throws Exception {
		// TODO 自动生成方法存根
		if(id!=null){
			this.weixinAccount = this.weixinAccountService.getWeixinAccountById( id );
		}else{
			this.weixinAccount = new WeixinAccount();
		}
		//HashMap<String,Operation> oper=this.getOperMapByUser(this.getCurrentUser());
		//PublicFunction.getOperationPopedom(oper,popedom,OperationConstants.CONFIG_WEIXINACCOUNT);		
	}


	public String list() throws Exception {

		DetachedCriteria query = DetachedCriteria.forClass(WeixinAccount.class);
		getDataList(query);
		return SUCCESS;
	}

	/*按页获取有效数据*/
	protected void getDataList(DetachedCriteria query ){
		
		List<WeixinAccount> result = new ArrayList<WeixinAccount>();
		query.add(Restrictions.eq("delete_flag", false));
		int totalRows=RequestUtils.getTotalRowsFromRequest(getRequest());
		if(totalRows<0)
		{
			totalRows= weixinAccountService.getWeixinAccountCountByCriteria(query);
		}
		int[] rowStartEnd=RequestUtils.getRowStartEnd(getRequest(),totalRows,DEFAULT_PAGE_SIZE,0);
		query.addOrder(Order.asc("id"));
		query.setProjection(null);
		Limit limit=RequestUtils.getLimit(getRequest(),"ec",totalRows,DEFAULT_PAGE_SIZE);
		int pageSize=limit.getCurrentRowsDisplayed();
		
		result.addAll((weixinAccountService.getWeixinAccountInfos(query, pageSize, rowStartEnd[0])));
		
		weixinAccountList.addAll(result);
		limit = RequestUtils.getLimit(getRequest(),"ec",result.size(),DEFAULT_PAGE_SIZE);
	}
	
	public String input(){
		//this.departId = this.getCurrentUser().getPeople().getDepart().getId();
		System.out.println("input+++");
		return INPUT;
	}
	

	public String save() throws ServletException
	{


		try {
			if (id == null) {
				weixinAccount.setInsert_oper(this.getCurrentUser());
				weixinAccount.setInsert_time(new Date());
				System.out.println("name=" + weixinAccount.getName());
				System.out.println("weixinnumber="
						+ weixinAccount.getWeixin_number());
				weixinAccountService.saveWeixinAccountInfo(weixinAccount,
						weixin_type_id, is_Certified_id, unit_id);
				this.addActionMessage(SystemMessage.SAVE_SUCCESS);
			} else {
				if (this.getVersion() == weixinAccount.getVersion()) {
					weixinAccount.setUpdate_oper(this.getCurrentUser());
					weixinAccount.setUpdate_time(new Date());
					weixinAccountService.updateWeixinAccountInfo(weixinAccount,
							weixin_type_id, is_Certified_id, unit_id);
					this.addActionMessage(SystemMessage.UPDATE_SUCCESS);

				} else
					this.addActionMessage(SystemMessage.UPDATE_FAIL);
			}
		} catch (UniqueConstraintException e) {
			this.addActionError(SystemMessage.SAVE_FAIL + e.getMessage());
			return SUCCESS;
		}
		showInfo();	
		
		return SUCCESS;
	}

		
	protected void showInfo() {
		if(this.weixinAccount.getWeixin_type()!=null)
			this.weixin_type_id = this.weixinAccount.getWeixin_type().getId();
		
		
		if(this.weixinAccount.getIs_Certified()!=null)
			this.is_Certified_id = this.weixinAccount.getIs_Certified().getId();
		
		if(this.weixinAccount.getUnitInfo()!=null){
			this.unit_id = this.weixinAccount.getUnitInfo().getId();
			this.unit_name = this.weixinAccount.getUnitInfo().getUnit_name();			
		}
	}
	
	public String edit(){
		weixinAccount = weixinAccountService.getWeixinAccountById(id);
		showInfo();
		return INPUT;
	}

	public String delete() {
		if (id != null) {
			try {
				weixinAccount = this.weixinAccountService
						.getWeixinAccountById(id);
				weixinAccount.setUpdate_oper(this.getCurrentUser());
				weixinAccount.setUpdate_time(new Date());
				weixinAccount.setDelete_flag(true);
				weixinAccountService.saveWeixinAccountInfo(weixinAccount);

			}
			catch (UniqueConstraintException e) {
				this.addActionMessage("删除失败！" + e.getMessage());
				return INPUT;
			}
		} else {
			this.addActionMessage(SystemMessage.NO_OBJECT);
			return INPUT;
		}
		return SUCCESS;
	}
	

	public String search() {
		DetachedCriteria query = DetachedCriteria.forClass(WeixinAccount.class);
			if (!StringTools.isNullOrSpace(this.qName)) {
				query.add(Restrictions.like("name", "%"+this.qName+"%"));
			}

			if (this.qweixin_type!=null) {
				query.createAlias("weixin_type", "weixin_type").add(Restrictions.eq("weixin_type.id", this.qweixin_type));
			}
			if (this.qis_Certified!=null) {
				query.createAlias("is_Certified", "is_Certified").add(Restrictions.eq("is_Certified.id", this.qis_Certified));
			}
			if (this.qunit_id!=null) {
				query.createAlias("unitInfo", "unitInfo").add(Restrictions.eq("unitInfo.id", this.qunit_id));
			}
		getDataList(query);
		return SUCCESS;
	}
	/**
	 * @return is_Certified_id
	 */
	public Long getIs_Certified_id() {
		return is_Certified_id;
	}
	/**
	 * @param is_Certified_id 要设置的 is_Certified_id
	 */
	public void setIs_Certified_id(Long is_Certified_id) {
		this.is_Certified_id = is_Certified_id;
	}
	/**
	 * @return weixin_type_id
	 */
	public Long getWeixin_type_id() {
		return weixin_type_id;
	}
	/**
	 * @param weixin_type_id 要设置的 weixin_type_id
	 */
	public void setWeixin_type_id(Long weixin_type_id) {
		this.weixin_type_id = weixin_type_id;
	}
	
	public String select() 
	{
		weixinAccountList.addAll(weixinAccountService.getWeixinAccountInfos());
		return SUCCESS;
	}	
}
